﻿using Microsoft.EntityFrameworkCore.Metadata;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GuanYijia.Application.ProductionManagement
{
    public class Send
    {


        public static async Task SimpleSendMsg()
        {
            string queueName = "simple_order"; //队列名

            // 创建连接
            using (IConnection? connection =  RabbitMQHelper.GetConnection())
            {
                // 创建信道
                using (IChannel? channel = await connection.CreateChannelAsync())
                {
                    // 创建队列
                    await channel.QueueDeclareAsync(queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);

                    int i = 0;
                    while (true)
                    {
                        string message = $"Hello RabbitMQ MessageHello,{i + 1}";
                        var body = Encoding.UTF8.GetBytes(message); //发送消息

                        // 创建一个 BasicProperties 对象
                        var basicProperties = new BasicProperties(); // 直接创建 BasicProperties 对象

                        //发送消息到默认交换机，队列名称作为路由键
                        await channel.BasicPublishAsync(
                            exchange: "",
                            routingKey: queueName,
                            mandatory: false,
                            basicProperties: basicProperties,
                            body: body);

                        Console.WriteLine($"发送消息到队列:{queueName}, 内容:{message}");
                        i++;

                        // 避免阻塞，使用异步延时
                        await Task.Delay(1000);
                    }
                }
            }
        }
    }
}
